#    Copyright 2012 New Mexico EPSCoR
#
#    This file is part of Teacher's P.E.T.
#
#    Teacher's P.E.T. is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Teacher's P.E.T. is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Teacher's P.E.T..  If not, see <http://www.gnu.org/licenses/>.

from django.shortcuts import HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth import views
from django.contrib.auth.models import User
from django.contrib import messages
from django.core.urlresolvers import reverse

from options import Options

def login(request, *args, **kwargs):
	if request.method == 'POST':
		username = request.POST['username']
		password = request.POST['password']
		next = request.POST['next']
	
		#check if user exists
		TheUser = User.objects.filter(username=username)
		user = None
		if TheUser:
			user = auth.authenticate(username=username, password=password)
			if user is not None:
				if user.is_active:
					auth.login(request, user)
			else:
				# Return a 'disabled account' error message
				messages.error(request,'This account has been disabled. If this is a new account, please request a password from the login form. Otherwise, please contact an isntructor or system administrator.')
		if not user:
			# Return an 'invalid login' error message.
			messages.error(request,'Login failed, please check your username and password and try again. If this problem persists, please contact a system administrator.')
	elif request.method == 'GET':
		next = request.GET['next']
	return HttpResponseRedirect(next)
	
def logout(request,*args,**kwargs):
	return views.logout(request)

def password_reset_done(request, *args, **kwargs):
	return views.password_reset_done(request)
	
def password_reset_complete(request, *args, **kwargs):
	return views.password_reset_complete(request)
	
def password_reset_confirm(request,uidb36,token,*args,**kwargs):
	return views.password_reset_confirm(request,uidb36,token,post_reset_redirect=reverse(password_reset_complete))
	
def password_reset(request):
	return views.password_reset(request,from_email=Options.get_option('Noreply Address',app='Contact'),post_reset_redirect=reverse(password_reset_done))

